import logging
import os
import webapp2

from db.stats_client import StatsClient
from google.appengine.api import users
from google.appengine.ext.webapp import template


class AdminPage(webapp2.RequestHandler):
  def get(self):
    stats_client = StatsClient()
    user = users.get_current_user()
    student_map = stats_client.getStudentMap()

    progress_stats = stats_client.getStudentsProgress(student_map)
    friend_survey_stats = stats_client.getFriendSurveyStats(student_map)
    ra_stats = stats_client.getStudentsByRa(student_map)
    dorm_stats = stats_client.getStudentsByDorm(student_map)

    total_taken = progress_stats[len(progress_stats) - 1].student_count

    if len(student_map.keys()) > 0:
      response_rate = \
          int((float(total_taken) / float(len(student_map.keys()))) * 100)
    else:
      response_rate = 0

    tmpl_data = {
      'progress_stats': progress_stats,
      'friend_survey_stats': friend_survey_stats,
      'ra_stats': ra_stats,
      'dorm_stats': dorm_stats,
      'response_rate': response_rate
    }
    path = os.path.join(os.path.dirname(__file__), 'admin.html')
    self.response.out.write(template.render(path, tmpl_data))


app = webapp2.WSGIApplication([
  ('/admin', AdminPage),
], debug=True)
